package com.k2data.k2app.common;

import com.google.common.collect.Lists;
import com.k2data.k2app.constant.Constant;
import com.k2data.k2app.domain.po.Customer;
import com.k2data.k2app.domain.po.Device;
import com.k2data.k2app.domain.po.TDevice;
import com.k2data.k2app.domain.po.TMonitorPoint;
import com.k2data.k2app.mapper.CustomerMapper;
import com.k2data.k2app.mapper.DeviceMapper;
import com.k2data.k2app.mapper.TDeviceMapper;
import com.k2data.k2app.mapper.TMonitorPointMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Objects;

/**
 * @author lidong9144@163.com 17-3-28.
 */
@Component
public class Validator {

    private final DeviceMapper deviceMapper;
    private final CustomerMapper customerMapper;
    private final TDeviceMapper tDeviceMapper;
    private final TMonitorPointMapper tMonitorPointMapper;

    @Autowired
    public Validator(DeviceMapper deviceMapper, CustomerMapper customerMapper,
                     TDeviceMapper tDeviceMapper, TMonitorPointMapper tMonitorPointMapper) {
        this.deviceMapper = deviceMapper;
        this.customerMapper = customerMapper;
        this.tDeviceMapper = tDeviceMapper;
        this.tMonitorPointMapper = tMonitorPointMapper;
    }

    /**
     * 判断 deviceId 是否存在
     *
     * @return {@code true} 存在 {@code false} 不存在
     */
    public boolean checkDeviceID(long deviceId) {
        Device device = new Device();
        device.setId(deviceId);
        device.setIsRoot(Constant.YES);

        return deviceMapper.checkExist(device) != null;
    }

    /**
     * 判断 componentId 是否存在
     *
     * @return {@code true} 存在 {@code false} 不存在
     */
    public boolean checkComponentId(long componentId) {
        Device device = new Device();
        device.setId(componentId);
        device.setIsRoot(Constant.NO);

        return deviceMapper.checkExist(device) != null;
    }

    /**
     * 判断 customerId 是否存在
     *
     * @return {@code true} 存在 {@code false} 不存在
     */
    public boolean checkCustomerID(long customerId) {
        Customer customer = new Customer();
        customer.setId(customerId);

        return customerMapper.checkExistByPrimaryKey(customer) != null;
    }

    /**
     * 检查所有 deviceId 是否都存在
     *
     * @param deviceIds 要检查的 deviceId 列表
     * @return 不存在的 deviceId
     */
    public List<Long> checkDeviceIds(List<Long> deviceIds) {
        Objects.requireNonNull(deviceIds);

        List<Long> exists = deviceMapper.selectIdsByIds(deviceIds);

        if (exists.size() == deviceIds.size()) {
            return Lists.newArrayList();
        }

        exists.removeAll(deviceIds);

        return exists;
    }

    public boolean checkTemplateDeviceId(long deviceId) {
        TDevice cond = new TDevice();
        cond.setId(deviceId);

        return tDeviceMapper.checkExist(cond) != null;
    }

    public boolean checkSensorType(String sensorType) {
        TMonitorPoint cond = new TMonitorPoint();
        cond.setSensorType(sensorType);

        return tMonitorPointMapper.checkExist(cond) != null;
    }

    public boolean checkSensorNameEn(String nameEn) {
        TMonitorPoint cond = new TMonitorPoint();
        cond.setNameEn(nameEn);

        return tMonitorPointMapper.checkExist(cond) != null;
    }

}
